import { createRouter, createWebHistory } from "vue-router";
import { ElMessage } from "element-plus"; // 引入element-plus的message组件

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "home",
      component: () => import("../components/MainBox.vue"),
      redirect: "/home",
      children: [
        {
          path: "/home",
          name: "lawTest",
          component: () => import("../views/menu/LawTest.vue"),
          meta: { title: "律法测试" },
        },
        {
          path: "/ai-chat",
          name: "aiChat",
          component: () => import("../views/menu/AiChatView.vue"),
          meta: { title: "AI问答" },
        },
        {
          path: "/analysis",
          name: "analysis",
          component: () => import("../views/menu/CaseAnalysis.vue"),
          meta: { title: "案件分析" },
        },
        {
          path: "/persion",
          name: "persion",
          component: () => import("../views/menu/Person.vue"),
          meta: { title: "个人中心" },
        },
        {
          path: "/ai-chat/:caseId",
          component: () => import("../views/pages/case/CaseChat.vue"),
          meta: { title: "案件分析详情" },
        },
        {
          path: '/record',
          component: () => import("../views/pages/practice/Record.vue"),
          meta: { title: '个人答题记录' }
        },
        {
          path: '/recordDetail/:recordNo',
          component: () => import("../views/pages/practice/RecordDetail.vue"),
          meta: { title: '个人答题记录详情' }
        }
      ],
    },
    {
      path: "/login",
      name: "login",
      component: () => import("../views/login/Login.vue"),
      meta: { title: "登录", noAuth: true },
    },
    {
      path: "/register",
      name: "register",
      component: () => import("../views/regist/Regist.vue"),
      meta: { title: "注册", noAuth: true },
    },
    {
      path: "/generateClaimReport",
      name: "generateClaimReport",
      component: () => import("../views/pages/generateClaimReport.vue"),
      meta: { title: "生成诉状报告"},
    },
    {
      path: "/practice/test",
      name: "PracticeTest",
      component: () => import("@/views/pages/practice/PracticeTest.vue"),
      meta: { title: "测试" },
    },
    {
      path: "/404",
      meta: {
        title: "找不到页面",
        noAuth: true,
      },
      component: () => import("../views/pages/404.vue"),
    },
    { path: "/:path(.*)", redirect: "/404" }, // 匹配所有未定义的路由，重定向到404页面
  ],
});

// 路由拦截
router.beforeEach((to, from, next) => {
  const role = localStorage.getItem("role-isAdmin");
  // 1. 获取本地存储的token（假设你登录后把token存在localStorage中）
  const token = localStorage.getItem("token");

  console.log("Route meta:", to.meta); // 检查当前路由的meta配置

  console.log("Token:", localStorage.getItem("token")); // 查看token是否正确存储

  // 2. 不需要认证的页面（如登录页、注册页等）直接放行
  if (to.meta.noAuth === true) {
    next();
    return;
  }

  // 3. 需要认证但无token的情况，重定向到登录页
  if (!token || token === "undefined" || token === null) {
    ElMessage.info("请先登录");

    next("/login");

    // 可选：存储原始目标路径，登录后可以跳转回来
    if (to.path !== "/login") {
      localStorage.setItem("redirectPath", to.fullPath);
    }
    return;
  }

  // 4. 需要认证且有token的情况，检查权限

  // 5. 所有检查通过，正常访问
  next();
});

export default router;
